<template>
  <el-container class="container">
    <el-header class="header">
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="signout"> 退出 </el-button>
    </el-header>

    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside class="aside" :width="isCollapse ? '64px' : '200px'">
        <div
          class="toggle_button"
          :class="!isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
          @click="isCollap"
        ></div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="skyblue"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="navpath"
        >
          <!-- 一级导航菜单 -->
          <!-- index不能为数字，item.id 是数字 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 一级菜单logo -->
              <i :class="iconList[item.id]"></i>
              <!-- 一级菜单名称， -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级导航菜单 -->

            <el-menu-item
              :index="'/' + submenu.path"
              v-for="submenu in item.children"
              :key="submenu.id"
              @click="getNavState('/' + submenu.path)"
            >
              <template slot="title">
                <!-- 二级菜单logo -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单名称， -->
                <span>{{ submenu.authName }}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="main">
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconList: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      isCollapse: false,
      // 被激活的链接地址
      navpath: "",
    };
  },
  created() {
    this.getMenuList();
    this.navpath = window.sessionStorage.getItem("navpath");
  },
  methods: {
    signout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有的登录菜单
    async getMenuList() {
      // 结解构赋值
      const { data: res } = await this.$http.get("menus");
      console.log(res);
      if (res.meta.status !== 200) return this.$Message.error(res.meta.msg);
      this.menuList = res.data;
    },
    // 点击按钮切换菜单展开与重叠
    isCollap() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存index状态
    getNavState(navpath) {
      window.sessionStorage.setItem("navpath", navpath);
      this.navpath = navpath;
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
}
.header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  font-size: 25px;
  color: #fff;
  align-items: center;
}
.header div {
  display: flex;
  align-items: center;
}
.header div span {
  margin-left: 15px;
}
.aside {
  background-color: #313743;
}
.toggle_button {
  width: 100%;
  font-size: 20px;
  height: 25px;
  line-height: 25px;
  text-align: center;
  background-color: #999999;
  color: #fff;
  box-sizing: border-box;
}
.aside i {
  margin-right: 8px;
}
.el-menu {
  border: 0px;
  /* background-color: pink; */
}
.main {
  background-color: #e9edf1;
}
</style>